//定义推送属性的各个数值对应状态
//定义消息状态
var messageStatusArray = {};
messageStatusArray['1'] = '及时推送';
messageStatusArray['2'] = '延时推送';
messageStatusArray['3'] = '草稿';
//定义消息类型
var messageTypeArray = {};
messageTypeArray['1'] = '普通消息推送';
messageTypeArray['2'] = '系统更新推送';
messageTypeArray['3'] = '活动推送';
//定义推送用户范围
var merchantCodeArray = {};
merchantCodeArray['2'] = '所有运营商';
merchantCodeArray['1'] = '电桩用户';
merchantCodeArray['3'] = '华体用户';
//定义推送系统端
var sysCodeStatusArray = {};
sysCodeStatusArray['1'] = '所有端';
sysCodeStatusArray['2'] = 'app端';
sysCodeStatusArray['3'] = '微信端';
//定义推送消息范围
var scopeStatusArray = {};
scopeStatusArray['1'] = '所有用户';
scopeStatusArray['2'] = '所有在线用户';
scopeStatusArray['3'] = '注册3个月用户';
scopeStatusArray['3'] = '其他自定义条件用户';
//定义推送消息用户性别
var accountSexArray = {};
accountSexArray['1'] = '全部';
accountSexArray['2'] = '男';
accountSexArray['3'] = '女';


//本地测试双向绑定数据
DZApp.controller("pushController",function($scope,$http, $rootScope, rootService, $state, $filter, $location){
	
	//清除旧数据，显示新数据
  $scope.filterPushList = function() {
    $scope.pushList = null;
    $('#table_1').DataTable().destroy();
  }
  //推送记录分页与搜索
  $scope.pageingSearch = function(){
    var indexLoad =layer.load(0);
    if($scope.messageStatus==undefined){
      $scope.messageStatus="";
    }
    if($scope.messageType==undefined){
      $scope.messageType="";
    }
    if($scope.merchantCode==undefined){
      $scope.merchantCode="2";
    }
    if($scope.sysCode==undefined){
      $scope.sysCode="2";
    }
    console.log($scope.messageStatus + "," +$scope.messageType+","+$scope.merchantCode+","+$scope.sysCode);
    var postData = {
      pageNo:$scope.paginationConf.currentPage
      /*pageSize:$scope.paginationConf.itemsPerPage,
      searchStr:$scope.searchStr,
      messageStatus:$scope.messageStatus,
      messageType:$scope.messageType,
      merchantCode:$scope.merchantCode,
      sysCode:$scope.sysCode8*/
    };
    //多次请求解决方式
    if($scope.paginationConf.currentPage === 0){
      $scope.paginationConf.currentPage = 1;
    };
    if(postData.pageNo===0){
					postData.pageNo = 1;
				}
				if(postData.searchStr===""||postData.searchStr===null||postData.searchStr===undefined){
				var url = "/manage/message/pushMessageList?pageNo="+postData.pageNo;//+"&pageSize="+postData.pageSize+"&searchStr=&state="+postData.messageStatus+"&messageType="+postData.messageType+"&sysCode="+postData.merchantCode;
				
				}else{
				var url = "/getSimplePushPageList?pageNo="+postData.pageNo+"&pageSize="+postData.pageSize+"&searchStr="+postData.searchStr+"&state="+postData.messageStatus+"&messageType="+postData.messageType+"&sysCode="+postData.merchantCode;
				
				}
    $http.get("/appserver" + url).success(function(data, status, headers, config) { 
			if(data.code==="200"){
		        layer.close(indexLoad);
		        $scope.paginationConf.totalItems = data.data.number;
		        $scope.pushData = data.data.messagesVOs;
		        $scope.setpushList();
		      }else{
		        layer.close(indexLoad);
		      }

		})
  }
  //处理推送数据
  $scope.setpushList=function(argument) {
  	$scope.pushList=[];
  	angular.forEach($scope.pushData, function(data,index,array){
			$scope.pushList.push(array[index]);
			$scope.pushList[index].scope=scopeStatusArray[array[index].scope];
			$scope.pushList[index].pushOperator=merchantCodeArray[array[index].pushOperator];
			$scope.pushList[index].sysCode=sysCodeStatusArray[array[index].sysCode];
			$scope.pushList[index].messageType=messageTypeArray[array[index].messageType];
			$scope.pushList[index].state=messageStatusArray[array[index].state];
			
		});
  }
  $scope.paginationConf = {
        currentPage: 1,
        itemsPerPage: 20
  };
  //监听筛选条件和搜索的变化，进行推送记录查询 + paginationConf.itemsPerPage + messageStatus + messageType + merchantCode + sysCode
  $scope.$watch('paginationConf.currentPage', $scope.pageingSearch);
  
  //更换消息状态
  /*$scope.changeMessageStatus = function($event) {
      $scope.messageStatus;
      alert($scope.messageStatus);
      $scope.searchStr = "";
      $scope.paginationConf.currentPage=1;
      $scope.fiterModel.messageStatus = $scope.messageStatus;
      $scope.pageingSearch();
    $scope.filterPushList();
  }*/
  //点击弹出新增推送窗口
  $scope.addPushMessage=function () {
     $scope.add_title="";
     $scope.add_content="";
     //默认显示第一个tag
    $scope.add_messageType = '1';
    $scope.add_sysCode = '1';
    $scope.add_scope = '1';
    $scope.add_pushOperator = '2';
    $scope.add_state = '1';
    var add_delayDateTime = new Date().getTime();
    $scope.add_delayDate = $filter('date')(add_delayDateTime, 'yyyy-MM-dd hh:mm:ss');
    $scope.dateIsChange = true;
     $('#subjectModel1').modal('show');
  }

  $scope.$watch('add_delayDate', function(newVal) {
      if (!!newVal && !$scope.dateIsChange) {
        $scope.add_delayDate = $filter('date')(new Date(newVal).getTime(), 'yyyy-MM-dd hh:mm:ss');
        $scope.dateIsChange = true;
      }
  });
  //默认显示第一个tag
  $scope.add_messageType = '1';
  $scope.add_sysCode = '1';
  $scope.add_scope = '1';
  $scope.add_pushOperator = '2';
  $scope.add_state = '1';
  var add_delayDateTime = new Date().getTime();
  $scope.add_delayDate = $filter('date')(add_delayDateTime, 'yyyy-MM-dd hh:mm:ss');
  $scope.dateIsChange = true;

  //点击保存并推送
  $scope.submitPushMessage=function () {
      //验证成功执行代码
      console.log('验证成功');
      if($scope.add_title!=undefined && $scope.add_content!=undefined){
      	//alert("不空");
	      if($scope.add_state!="3"){
	        $scope.add_delayDate="";
	      }
	      var postData={
	        pushId:'',
	        title:$scope.add_title,
	        content:$scope.add_content,
	        editDate:'',
	        pushDate:'',
	        pushUserId:$scope.userData.userName,
	        isPush:'',
	        onLineUser:'',
	        messageType:$scope.add_messageType,
	        sysCode:$scope.add_sysCode,
	        scope:$scope.add_scope,
	        pushOperator:$scope.add_pushOperator,
	        state:$scope.add_state,
	        delayDate:$scope.add_delayDate,
	        regStartDate:$scope.add_regStartDate,
	        regEndDate:$scope.add_regEndDate,
	        accountSex:$scope.add_accountSex
	      }
	      var indexLoad =layer.load(0);
	      var url = "/manage/message/savePushMessage";
	      $http({
				method: 'POST',
				url: "/appserver" + url,
				data: $.param(postData),
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
				},
				cache: false
			}).success(function(data, status, headers, config) {
				layer.close(indexLoad);
		        if ("200" == data.code) {
		          bootboxWindow.alert("保存推送消息成功",function(){
		            window.location.reload();
		          });
		          //$('#addPushMessage').modal('hide');
		        } else {
		          bootboxWindow.alert("保存推送消息失败："+ data.msg, null);
		        }
			});
	}else{
		bootboxWindow.alert("请填写推送标题和内容", null);
	}
  }
  //点击预览
  $scope.previewPushMessage=function (argument) {
    var pushOperatorStr='';
    if($scope.add_pushOperator=="2"){
        pushOperatorStr=""
    }else if($scope.add_pushOperator=="1"){
        pushOperatorStr="电桩"
    }else if($scope.add_pushOperator=="3"){
        pushOperatorStr="华体"
    }
    $scope.previewTitle="尊敬的"+pushOperatorStr+"用户：";
    $scope.previewContent=$scope.add_content;
    $('#previewPushMessage').modal('show');
  }
  //查看消息
  $scope.queryPushMessage=function(pushId) {
  		var url = "/manage/message/messageDatail?pushId=" + pushId;
  		$http.get("/appserver" + url).success(function(data, status, headers, config) { 
			if ("200" == data.code) {
          $scope.pushDetail=data.data;
          $scope.pushDetail.scope=scopeStatusArray[data.data.scope];
			$scope.pushDetail.push_operator=merchantCodeArray[data.data.push_operator];
			$scope.pushDetail.sys_code=sysCodeStatusArray[data.data.sys_code];
			$scope.pushDetail.message_type=messageTypeArray[data.data.message_type];
			$scope.pushDetail.state=messageStatusArray[data.data.state];
          $('#subjectModel2').modal('show');
        } else {
          bootboxWindow.alert("推送详情查询失败："+ data.msg, null);
        }
			});
  }
  //删除消息
  $scope.deleteMessage=function (pushId) {
    bootboxWindow.confirm("您确定删除此消息？", function() {
      var indexLoad =layer.load(0);
      var url = "/manage/message/deleteMessage?pushId=" + pushId;
      $http.delete("/appserver" + url).success(function(data, status, headers, config) { 
			layer.close(indexLoad);
	        if ("200" == data.code) {
	          bootboxWindow.alert("消息删除成功", function () {
	              $scope.pageingSearch();
	          });
	        } else {
	          bootboxWindow.alert("消息删除失败:" + data.msg, null);
	        }
			})
    }, null);
  }
});